using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Scintilla.Configuration.SciTE;
using UmbracoXsltEditor.Properties;
using UmbracoXsltEditor.Util;
using System.Reflection;

namespace UmbracoXsltEditor.Forms
{
    public partial class Main : Form
    {
        public static bool firstLoad = true;

        public Main()
        {
            InitializeComponent();

            Icon = Resources.UmbracoIcon;
            editor.Configuration = ScintillaHelper.CreateScintillaConfig();
            editor.ConfigurationLanguage = "XML";
            input.Configuration = ScintillaHelper.CreateScintillaConfig();
            input.ConfigurationLanguage = "XML";
            input.IsReadOnly = true;
            output.Configuration = ScintillaHelper.CreateScintillaConfig();
            output.ConfigurationLanguage = "XML";
            output.IsReadOnly = true;
        }

        protected void Connect()
        {
            ConnectBox connect = new ConnectBox();
            DialogResult result = connect.ShowDialog();
            connect.Dispose();

            if(result == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    Databind_ddlXsltFiles();
                    Databind_UmbracoXml();
                }
                catch(Exception ex)
                {
                    //Connect();
                    string location = Assembly.GetExecutingAssembly().Location;

                    MessageBox.Show(ex.Message + "\n "
                        + ex.StackTrace + "\n "
                        + location);
                }

            }
        }

        #region Databinding
        protected void Databind_ddlXsltFiles()
        {
            string[] files = UmbracoHelper.GetXsltFileList(Settings.Default.Username, Settings.Default.Password);

            ddlXsltFiles.Items.Clear();
            foreach (string file in files)
            {
                ddlXsltFiles.Items.Add(file);
            }

            if(ddlXsltFiles.Items.Count > 0)
                ddlXsltFiles.SelectedIndex = 0;
        }

        protected void Databind_UmbracoXml()
        {
            //Get the Umbraco XML file.
            byte[] file = UmbracoHelper.GetUmbracoXml(Settings.Default.Username, Settings.Default.Password);
            string strFile = Encoding.UTF8.GetString(file);

            //Bind it to the input
            input.IsReadOnly = false;
            input.Text = strFile;
            input.IsReadOnly = true;

            //Bind the list of nodes to the currentPage dropdown
            ddlCurrentPage.Items.Clear();
            Dictionary<string, string> list = UmbracoHelper.GetNodeTree(strFile);

            foreach (KeyValuePair<string, string> pair in list)
            {
                ddlCurrentPage.Items.Add(pair);
            }

            if(ddlCurrentPage.Items.Count > 0)
                ddlCurrentPage.SelectedIndex = 0;
        }
        #endregion

        #region Menu Buttons

        private void connectStripMenuItem_Click(object sender, EventArgs e)
        {
            Connect();
        }
        
        private void aboutToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
            about.Dispose();
        }

        

        private void quitToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }
        #endregion

        #region Toolbar dropdowns/buttons

        private void ddlXsltFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strFile = (string)ddlXsltFiles.SelectedItem;
            if(strFile != string.Empty)
            {
                byte[] file = UmbracoHelper.GetXsltFile(Settings.Default.Username, Settings.Default.Password, strFile);
                editor.Text = Encoding.UTF8.GetString(file);
                
                //Switch to the XSLT view
                tabs.SelectedIndex = 0;
            }
        }

        private void tsbTransform_Click(object sender, EventArgs e)
        {
            if(ddlCurrentPage.SelectedItem != null)
            {
                //string currentid
                //    = ((KeyValuePair<string, string>) ddlCurrentPage.SelectedItem).Value;
                string currentid
                    = ((KeyValuePair<string, string>)ddlCurrentPage.SelectedItem).Key;
                string txtOutput = UmbracoHelper.Transform(editor.Text, input.Text, currentid);

                output.IsReadOnly = false;
                output.Text = txtOutput;
                output.IsReadOnly = true;

                //Switch to the ouput view
                tabs.SelectedIndex = 2;
            }
        }
        #endregion

        private void Main_Activated(object sender, EventArgs e)
        {
            if(firstLoad)
            {
                firstLoad = false;
                //Connect
                Connect();
            }
        }
    }
}